package com.cdn.nodes.controller;

import java.util.List;

import com.cdn.nodes.domain.dto.EdgeNodeCheckSshDTO;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cdn.common.log.annotation.Log;
import com.cdn.common.log.enums.BusinessType;
import com.cdn.common.security.annotation.RequiresPermissions;
import com.cdn.nodes.domain.EdgeNodeSsh;
import com.cdn.nodes.service.IEdgeNodeSshService;
import com.cdn.common.core.web.controller.BaseController;
import com.cdn.common.core.web.domain.AjaxResult;
import com.cdn.common.core.web.page.TableDataInfo;

/**
 * 节点SSH，用于管理边缘节点的SSH密钥和访问权限Controller
 * 
 * @author Cinzia
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/ssh")
public class EdgeNodeSshController extends BaseController
{
    @Autowired
    private IEdgeNodeSshService edgeNodeSshService;

    /**
     * 查询节点SSH，用于管理边缘节点的SSH密钥和访问权限列表
     */
    @RequiresPermissions("nodes:ssh:list")
    @GetMapping("/list")
    public TableDataInfo list(EdgeNodeSsh edgeNodeSsh)
    {
        startPage();
        List<EdgeNodeSsh> list = edgeNodeSshService.selectEdgeNodeSshList(edgeNodeSsh);
        return getDataTable(list);
    }

//    /**
//     * 导出节点SSH，用于管理边缘节点的SSH密钥和访问权限列表
//     */
//    @RequiresPermissions("nodes:ssh:export")
//    @Log(title = "节点SSH，用于管理边缘节点的SSH密钥和访问权限", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, EdgeNodeSsh edgeNodeSsh)
//    {
//        List<EdgeNodeSsh> list = edgeNodeSshService.selectEdgeNodeSshList(edgeNodeSsh);
//        ExcelUtil<EdgeNodeSsh> util = new ExcelUtil<EdgeNodeSsh>(EdgeNodeSsh.class);
//        util.exportExcel(response, list, "节点SSH，用于管理边缘节点的SSH密钥和访问权限数据");
//    }

    /**
     * 获取节点SSH，用于管理边缘节点的SSH密钥和访问权限详细信息
     */
    @RequiresPermissions("nodes:ssh:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(edgeNodeSshService.selectEdgeNodeSshById(id));
    }

    /**
     * 新增节点SSH，用于管理边缘节点的SSH密钥和访问权限
     */
    @RequiresPermissions("nodes:ssh:add")
    @Log(title = "节点SSH，用于管理边缘节点的SSH密钥和访问权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EdgeNodeSsh edgeNodeSsh)
    {
        return toAjax(edgeNodeSshService.insertEdgeNodeSsh(edgeNodeSsh));
    }

    /**
     * 修改节点SSH，用于管理边缘节点的SSH密钥和访问权限
     */
    @RequiresPermissions("nodes:ssh:edit")
    @Log(title = "节点SSH，用于管理边缘节点的SSH密钥和访问权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EdgeNodeSsh edgeNodeSsh)
    {
        return toAjax(edgeNodeSshService.updateEdgeNodeSsh(edgeNodeSsh));
    }


    /**
     * 测试SSH连接
     */
    @RequiresPermissions("nodes:ssh:add")
    @Log(title = "测试SSH连接")
    @PostMapping("/check")
    public AjaxResult checkSsh(@RequestBody EdgeNodeCheckSshDTO edgeNodeCheckSshDTO)
    {
        return edgeNodeSshService.checkSsh(edgeNodeCheckSshDTO)==0?success():error("连接出错,请重试...");
    }

    /**
     * 删除节点SSH，用于管理边缘节点的SSH密钥和访问权限
     */
    @RequiresPermissions("nodes:ssh:remove")
    @Log(title = "节点SSH，用于管理边缘节点的SSH密钥和访问权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(edgeNodeSshService.deleteEdgeNodeSshByIds(ids));
    }

    /**
     * 根据节点Id获取SSH认证信息
     */
    @GetMapping("/getSshByNodeId/{nodeIds}")
    public AjaxResult getSshByNodeIds(@PathVariable Long[] nodeIds)
    {
        return success(edgeNodeSshService.getSshByNodeId(nodeIds));
    }

    /**
     * 根据节点Id获取主机端口
     */
    @GetMapping("/getPortByNodeId/{nodeId}")
    public AjaxResult getPortByNodeId(@PathVariable Long nodeId){
        return success(edgeNodeSshService.getPortByNodeId(nodeId));
    }

}
